﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Application.MainModule.GroupBranchesManagement;
using Domain.MainModule.Entities;
using Presentation.Web.MVC.Client.ViewModels;

namespace Presentation.Web.MVC.Client.Controllers
{
    public class GroupBranchController : Controller
    {
        #region private member

        /// <summary>
        /// Instance of the group branch service.
        /// </summary>
        private IGroupBranchManagementService _groupBranchService;

        #endregion

        #region constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="GroupBranchController"/> class.
        /// </summary>
        /// <param name="groupBranchService">The group branch service.</param>
        public GroupBranchController(IGroupBranchManagementService groupBranchService)
        {
            _groupBranchService = groupBranchService;
        }

        #endregion

        #region public methods

        /// <summary>
        /// Display a paged listing of the existing group branchs.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">The page size.</param>
        /// <returns></returns>
        public ActionResult Index(int page, int pageSize)
        {
            IList<GroupBranch> groupBranches = _groupBranchService.FindPagedGroupBranches(page, pageSize);
            return View(new GroupBranchListViewModel(groupBranches, page, pageSize));
        }

        /// <summary>
        /// Shows a specific group branch in detail.
        /// </summary>
        /// <param name="groupBranchId">The group branch id.</param>
        /// <returns>A view of the group branch details.</returns>
        public ActionResult Details(Guid groupBranchId)
        {
            //Get the group branch aggregate.
            GroupBranch groupBranch = _groupBranchService.FindGroupBranchByGroupBranchId(groupBranchId);
            //Display its details.
            return View(groupBranch);
        }

        /// <summary>
        /// Displays a form to create a new group branch.
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// Creates the specified group branch based on the request data and displays it.
        /// </summary>
        /// <param name="customer">The group branch to add.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(GroupBranch groupBranch)
        {
            //Check if there aren't validation errors on the customer.
            if (ModelState.IsValid)
            {
                //Add group branch to the data base.
                _groupBranchService.AddGroupBranch(groupBranch);
                //Show the first page of the group branch.
                return RedirectToAction("Index", new { page = 0, pageSize = 10 });
            }
            else
            {
                //Return the Create view and display the validation errors.
                return View();
            }
        }

        /// <summary>
        /// Displays a form to edit the group branch.
        /// </summary>
        /// <param name="groupBranchId">The group branch code.</param>
        /// <returns></returns>
        public ActionResult Edit(Guid groupBranchId)
        {
            //Get the group branch aggregate to edit.
            GroupBranch groupBranch = _groupBranchService.FindGroupBranchByGroupBranchId(groupBranchId);
            //Return the
            return View(groupBranch);
        }

        /// <summary>
        /// Saves the changes on the editions performed to the specified group branch.
        /// </summary>
        /// <param name="groupBranch">The group branch.</param>
        /// <returns>The details of the group branch.</returns>
        [HttpPost]
        public ActionResult Edit(GroupBranch groupBranch)
        {
            if (ModelState.IsValid)
            {
                _groupBranchService.ChangeGroupBranch(groupBranch);
                return RedirectToAction("Details", new RouteValueDictionary() { { "groupBranchId", groupBranch.GroupBranchId } });
            }
            else
            {
                return View();
            }
        }

        /// <summary>
        /// Deletes the specified group branch.
        /// </summary>
        /// <param name="groupBranchId">The group branch code.</param>
        /// <returns>The details of the group branch.</returns>
        public ActionResult Delete(Guid groupBranchId)
        {
            GroupBranch groupBranch = _groupBranchService.FindGroupBranchByGroupBranchId(groupBranchId);
            _groupBranchService.RemoveGroupBranch(groupBranch);
            
            return RedirectToAction("Index", new RouteValueDictionary() { { "page", 0 }, { "pageSize", 10 } });
        }

        #endregion
    }
}
